package com.javaee.controller.user;

import com.javaee.BeforeBaseController;
import com.javaee.entity.Record;

import com.javaee.entity.User;
import com.javaee.service.user.URecordService;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
@RequestMapping("/user")
public class URecordController extends BeforeBaseController {
    @Autowired
    private URecordService uRecordService;
    @RequestMapping("/showURecordList")
    public String showDRecordList(HttpSession session, Model model, @RequestParam(defaultValue = "1")Integer currentPage){
        User user = ( User) session.getAttribute("User");
        int uId =  user.getId();
        int totalCount =uRecordService.selectCount(uId);
        int pageSize = 5;
        int totalPage = (int)Math.ceil(totalCount*1.0/pageSize);
        List<Record> records=uRecordService.findByUserId((currentPage-1)*pageSize, pageSize,uId);
        model.addAttribute("totalPage", totalPage);
        model.addAttribute("currentPage", currentPage);
        model.addAttribute("records", records);
        return "user/showRecordList";
    }
    @RequestMapping("/showADetail")
    public String showDetail(Model model,int id){
        Record record=uRecordService.findARecordById(id);
        String detail=record.getDetail();
        String htmlDetail = uRecordService.convertMarkdownToHtml(detail);
        model.addAttribute("detail",htmlDetail);
        return "user/uShowARecordDetail";
    }
}
